<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>
    <input id="dec-number" type="number" placeholder="输入一个十进制非负整数">
    <button id="trans-btn">转化为二进制</button>
    <p id="result">运算结果</p>
    <script>
        function dec2bin(decNumber) {
            // 在这里实现你的转化方法，注意需要判断输入必须为一个非负整数
            console.log(typeof Number(decNumber))
            if (Number(decNumber) <= 0 || !Number.isInteger(Number(decNumber))) {
                alert('请输入正整数');
                return '';
            }
            console.log(parseInt(decNumber));
            let result = ''
            while (decNumber != 0) {
                let left = decNumber % 2;
                decNumber = Math.floor(decNumber / 2);
                result += left;
            }
            return result.split('').reverse().join('');
        }

        // 实现党点击转化按钮时，将输入的十进制数字转化为二进制，并显示在result的p标签内
        // Some coding
        var btn = document.getElementById('trans-btn');
        btn.onclick = function() {
            var p = document.getElementById('result');
            p.innerHTML = '运算结果：' + dec2bin(document.getElementById('dec-number').value);
        }
    </script>
</body>

</html>